import undetected_chromedriver as uc
from lxml import etree
import pymysql
from dbse.mysql.db_mysql_insert import addStockNews
from dbse.mysql.db_entity_stock_news import StockNews
from selenium.webdriver import ChromeOptions
import time
from selenium.webdriver.chrome.service import Service

# # 1
driver_path = r'C:\Program Files\Google\Chrome\Application\chromedriver.exe'
# service = Service(executable_path=driver_path)
# driver = webdriver.Chrome(service=service)
# # driver = webdriver.Chrome()
# driver.get('https://www.baidu.com')
# print(driver.page_source)
# driver.quit()
# 2
option = ChromeOptions()
option.add_argument('--headless')
# browser = uc.Chrome(options=option)
browser = uc.Chrome(driver_executable_path=driver_path,options=option)
browser.set_window_size(1366,768)
browser.get('https://www.egsea.com/news/flash.html')
print('====begin====')
# driver_path = r'C:\Program Files\Google\Chrome\Application\chromedriver.exe'
# service = Service(executable_path=driver_path)
# browser = uc.Chrome(service=service)
# browser.get('https://www.egsea.com/news/flash.html')
last_total = 0
while True:
    time.sleep(60)
    print("======================get news==========================")
    html = etree.fromstring(browser.page_source,etree.HTMLParser())
    urls = html.xpath('//div[contains(@class,"share-popup")]//@data-url')
    total = len(urls)
    add_num = total - last_total
    last_total = total
    # for item in urls:
    #     print(item)

    titles = html.xpath('//div[contains(@class,"share-popup")]//@data-title')
    # for item in titles:
    #     print(item)

    # 多个class 选择器
    descs = html.xpath('//div[contains(@class,"share-popup")]//@data-description')
    # for item in descs:
    #     print(item)

    newsTimes = html.xpath('//ul//div[@class="time"]//text()')
    # for item in newsTimes:
    #     print(item)

    # print("================================================")
    # print(urls[0])

    if add_num > 0:
        print("======================news insert db==========================")
        db = pymysql.connect(host='39.98.82.109',user='root',password='landleaf@123.com',port=3306,db='stock')
        sql = 'INSERT INTO stock_news (title, content,channel,url) VALUES (%s, %s,%s,%s)'
        # for index,item in enumerate(urls):
        #     print(titles[index])
        #     stockNews = StockNews(titles[index],descs[index],'e网',urls[index])
        #     addStockNews(db,sql,stockNews)
        for index in range(add_num):
            stockNews = StockNews(titles[index],descs[index],'e网',urls[index])
            addStockNews(db,sql,stockNews)
        db.close()


# html = etree.fromstring(browser.page_source,etree.HTMLParser())
# # result = html.xpath('//div[@class="left"]//ul//a//text()')
# # result = html.xpath('//div[@class="left"]//ul//div[@class="share-popup"]//@data-url')
# urls = html.xpath('//div[contains(@class,"share-popup")]//@data-url')
# for item in urls:
#     print(item)
#
# titles = html.xpath('//div[contains(@class,"share-popup")]//@data-title')
# for item in titles:
#     print(item)
#
# # 多个class 选择器
# descs = html.xpath('//div[contains(@class,"share-popup")]//@data-description')
# for item in descs:
#     print(item)
#
# newsTimes = html.xpath('//ul//div[@class="time"]//text()')
# for item in newsTimes:
#     print(item)
#
# print("================================================")
# print(urls[0])
#
# db = pymysql.connect(host='39.98.82.109',user='root',password='landleaf@123.com',port=3306,db='stock')
# sql = 'INSERT INTO stock_news (title, content,channel,url) VALUES (%s, %s,%s,%s)'
# for index,item in enumerate(urls):
#     print(titles[index])
#     stockNews = StockNews(titles[index],descs[index],'e网',urls[index])
#     addStockNews(db,sql,stockNews)
#




